import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;



public class Main {

	@SuppressWarnings("resource")
	public static void main(String[] args)
	{
		// make sure we get only one parameter - the file name
		if(args.length == 1)
		{
			// get the filename
			String filename = args[0];
			try
			{
				// read the file
				BufferedReader reader = new BufferedReader(new FileReader(filename));
				String line;			

				// for each line, convert a postﬁx expression to inﬁx form and evaluate the correct numerical value
				while ((line = reader.readLine()) != null)
				{
					Calc calc = new Calc();
					Calc.State curState = calc.process(line);
					if (curState.isError)					
						System.out.println(line + " : invalid expression");
					else
					{					
						String infixExp = curState.infixExpStack.pop(); 
						System.out.println(line + " : " + infixExp.substring(1, infixExp.length()-1) + " = " + curState.valueStack.pop());
					}
				}			
			}
			catch (Exception e)
			{
				System.err.format("Exception occurred trying to read '%s'.", filename);
				e.printStackTrace();			   
			}
		}
	}
}
